package com.interfaz.tabla.jcb;

import java.awt.Component;
import java.awt.ItemSelectable;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;

import javax.swing.AbstractCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.controlador.ElementoController;
import com.help.Categoria_Elemento_Renderer;
import com.interfaz.tabla.MyTBModel;
import com.modelo.Categoria_Elemento;

public class JCCategoriaEditor extends AbstractCellEditor implements TableCellEditor{
	JComboBox jcbCat;
	
	Object valorActual;
	Categoria_Elemento categoria = null;
	ElementoController elemento_controller;
	Vector<Categoria_Elemento> vector_categoria;
	
	public JCCategoriaEditor(){
		System.out.println("JCCategoriaEditor() constructor");
		elemento_controller = new ClassPathXmlApplicationContext("applicationContext.xml").getBean("elementoService",ElementoController.class);
		vector_categoria = new Vector<Categoria_Elemento>();
		vector_categoria = elemento_controller.llenarCategoriaElementoNuevo();
		jcbCat = new JComboBox(vector_categoria);
		jcbCat.setRenderer(new Categoria_Elemento_Renderer());
		ItemListener itemL = new ItemListener(){
										public void itemStateChanged(ItemEvent ie){
											if(ie.getStateChange()==ItemEvent.SELECTED){
												//ItemSelectable itemS = ie.getItem();
												Categoria_Elemento el_cat = new Categoria_Elemento();
												el_cat = (Categoria_Elemento)ie.getItem();
												if(el_cat!=null){
													System.out.println("JCCategoriaEditor: "+el_cat.getNombre_categoria());
													valorActual = el_cat;
													
												}else{
													System.out.println("JCCategoriaEditor: "+el_cat.getNombre_categoria());

												}
																								
											}
										}
							};
		jcbCat.addItemListener(itemL);
	}
	@Override
	public Object getCellEditorValue() {
		// TODO Auto-generated method stub
		return valorActual;
		
	}

	@Override
	public Component getTableCellEditorComponent(JTable tabla, Object objeto,
			boolean arg2, int nrow, int ncol) {
		// TODO Auto-generated method stub
		MyTBModel m = (MyTBModel)tabla.getModel();
		
		//categoria = m.get
		return jcbCat;
	}
	
}
